﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace DraggableWindow
{
    public partial class Page : UserControl
    {
		public List<DragDropControl> list = new List<DragDropControl>();
		int windowCount;
		List<People> people = new List<People>();
        public Page()
        {           
            InitializeComponent();
			people.Add(new People { LastName = "LastName1", FirstName = "FirstName1", Age = 1, Sex = "Male", Title = "Son" });
			people.Add(new People { LastName = "LastName1", FirstName = "FirstName2", Age = 5, Sex = "Female", Title = "Daughter" });
			people.Add(new People { LastName = "LastName1", FirstName = "FirstName3", Age = 35, Sex = "Male", Title = "Father" });
			people.Add(new People { LastName = "LastName1", FirstName = "FirstName4", Age = 34, Sex = "Female", Title = "Mother" });
            people.Add(new People { LastName = "LastName5", FirstName = "FirstName5", Age = 4, Sex = "Female", Title = "Daughter" });			
        }
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
        }
		void NewWindow(bool Modal)
		{
			windowCount++;
            ModalPopupWindow pw = new ModalPopupWindow();          
			pw.Title = "Test Window " + windowCount.ToString();
            pw.PopupClosed  += new EventHandler(pw_CloseButtonClicked);
			
            pw.OffsetX = 100 + windowCount * 10;
            pw.OffsetY = 100 + windowCount * 10;
            pw.HorizontalContentAlignment = HorizontalAlignment.Stretch;
            pw.VerticalAlignment = VerticalAlignment.Stretch;
            pw.MaxWidth = 500;
            pw.MaxHeight = 500;
			pw.Content = NewDateGrid();                             
            LayoutRoot.Children.Add(pw); // Make sure add ModalDialog to the Page Root.                			
            pw.Show();
		}

        void pw_CloseButtonClicked(object sender, EventArgs e)
        {
            PopupWindow pw = sender as PopupWindow;            
            MessageBox.Show(pw.Title + " Close Button is clicked");
            pw.Close();
            LayoutRoot.Children.Remove(pw);            
        }
       
		void btnNew_Click(object sender, RoutedEventArgs e)
		{
			NewWindow(false);
		}
		
		DataGrid NewDateGrid()
		{            
			DataGrid dg = new DataGrid();
            dg.AutoGenerateColumns = true;
			dg.ItemsSource = people;
            dg.HorizontalAlignment = HorizontalAlignment.Stretch;
            dg.VerticalAlignment = VerticalAlignment.Stretch;
			return dg;
		}
		TextBox NewTextBox(string text)
		{
			TextBox tb = new TextBox();
			
			tb.AcceptsReturn = true;
			tb.Text = text;
			return tb;
		}

		private void btnClear_Click(object sender, RoutedEventArgs e)
		{
            foreach (UIElement u in LayoutRoot.Children)
            {                
                DragDropControl dd = u as DragDropControl;
                if(dd != null)
                    list.Add(dd);   
            }
            foreach (UIElement u in list)
            {
                LayoutRoot.Children.Remove(u);
            }                                        
		}

		private void btnRestore_Click(object sender, RoutedEventArgs e)
		{
            if (LayoutRoot.Children.Count > 0)
                btnClear_Click(null, null);
            foreach (DragDropControl u in list)
			{
				u.Visibility = Visibility.Visible;                
				if (!LayoutRoot.Children.Contains(u))
				{
					PopupWindow old = u as PopupWindow;
					if(old != null)
					{
						PopupWindow pw = new PopupWindow();
						pw.Title = old.Title;
                        pw.OffsetX = old.OffsetX;
                        pw.OffsetY = old.OffsetY;
						pw.Width = (double)old.GetValue(FrameworkElement.WidthProperty);
						pw.Height = (double)old.GetValue(FrameworkElement.HeightProperty);
						//pw.IsModal = old.IsModal;
						pw.Content = NewDateGrid();
						LayoutRoot.Children.Add(pw);						
					}
				}               
			}
            list.Clear();
		}

		private void Button_Click(object sender, RoutedEventArgs e)
		{
			NewWindow(true);
		}
    }
	public class People
	{
		public string LastName { get; set; }
		public string FirstName { get; set; }
		public int Age { get; set; }
		public string Title { get; set; }
		public string Sex { get; set; }
	}
}
